package TextRank;

import dataStructure.ArffFuntion;
import dataStructure.Function;

import java.util.List;

public class Match {
    /*
    * 代码行匹配api调用模式，匹配成功更成api调用模式
    * @param1 fun 一个方法
    * @param2 str 需要更改的字符串代码行
    * @param3 list  api调用模式
    * */
    public  String Match(Function fun, String str, List<ArffFuntion> list){
        String result = str;
        for (ArffFuntion arf : list) {
            if (arf.getClassname().equals(fun.getClassname()) && arf.getMethodname().equals(fun.getFunname())){   //同一个类同一个方法
                for (String temp:arf.getList()) {
                    if (IsApi(str,temp)){
                        result = temp;
                    }
                }
            }
        }
         return result;
    }

    /*
    * 判断某个代码行是否有可以替换的api调用模式
    * */
    public boolean IsApi(String str , String api){
        boolean result = false;
        String [] arrapi = api.split("\\.");
        if (str.contains("."+arrapi[arrapi.length-1])){
            result = true;
        }
        if(arrapi[arrapi.length-1] .contains("init")){
            if(str.contains("new " + arrapi[arrapi.length-2])){
                result = true;
            }
        }
        return result;
    }

    /*public static void main(String[] args) throws Exception {
        String classname = "BulkReply";
        String funbody = "";
        int linenum = 10;
        String funname = "write";
        Function test1 = new Function(classname,funbody,linenum,funname);
        String str = "os.writeByte(MARKER);";

        List<ArffFuntion> test2 = new ArrayList<>();
        test2 = new DealArff().DealArff(("resource/netty.arff"));
        Match ma = new Match();
        String result = ma.Match(test1,str,test2);
        System.out.println(result);


        String a = test2.get(9).getClassname();
        String b = test2.get(9).getMethodname();
        System.out.println(a);
        System.out.println(b);

        String aa = test1.getClassname();
        String bb = test1.getFunname();
        System.out.println(aa);
        System.out.println(bb);

        System.out.println(a == aa);
        System.out.println(b == bb);

        System.out.println(a.equals(aa));
        System.out.println(b.equals(bb));

        System.out.println(test2.get(9).getList());
    }*/
}
